import router from '@system.router';
import dataStorage from '@ohos.data.storage'
import UserApi from '../../api/userApi.js'

export default {
    data: {
        width: 0,
        checked: false,
        isChecked: false,
        //        history: ''
    },
    onInit() {
        //        this.history = router.getParams().history;
    },
    dragStart(e) {
    },
    drag(e) {
        if (this.checked) {
            return;
        }
        this.width = e.globalX;
    },
    dragEnd(e) {
        if (this.width < 295) {
            this.width = 0;
        } else {
            this.width = 300;
            this.checked = true;
            this.isChecked = false;
        }
    },
    onSubmit(result) {
        if (!this.checked) {
            this.isChecked = true;
            return;
        }
        let username = result.value.username;
        let password = result.value.password;
        let user = {
            username, password
        };
        let loadingDialog = this.$child('dialogComp').$element('loadingDialog');
        console.info("zzxdw="+loadingDialog)
        UserApi.login(user, loadingDialog,(data) => {
            let storage = dataStorage.getStorageSync(this.$app.$def.globalData.path);
            storage.putSync('user', JSON.stringify(data.user));
            storage.flushSync();
            this.$app.$def.globalData.user = data.user;
            storage.putSync('studentId', JSON.stringify(data.user.id));
            storage.flushSync();
            router.replace({
                uri: 'pages/main/main'
            });
        })
    },
    register(){
        router.push({
            uri: 'pages/register/register'
        });
    }
}
